import subprocess
import time
import random

class ADBController:
    def __init__(self, adb_path=r".\\adbTools\\new\\adb.exe"):
        self.adb_path = adb_path

    def _execute(self, cmd):
        """执行ADB命令并返回输出"""
        full_cmd = f'"{self.adb_path}" {cmd}'  # 处理路径空格
        result = subprocess.run(
            full_cmd, 
            shell=True, 
            capture_output=True, 
            text=True
        )
        return result.stdout.strip()

    def tap(self, x, y, humanize=True):
        """模拟点击"""
        if humanize:  # 人性化随机偏移
            x += random.randint(-2, 2)
            y += random.randint(-2, 2)
            time.sleep(random.uniform(0.1, 0.3))
        return self._execute(f'shell input tap {x} {y}')

    def swipe(self, x1, y1, x2, y2, duration=400):
        """模拟滑动（duration单位：ms）"""
        return self._execute(
            f'shell input swipe {x1} {y1} {x2} {y2} {duration}'
        )
    def send_key(self, keycode):
        """发送按键事件"""
        subprocess.run(
            [self.adb_path, "shell", "input", "keyevent", str(keycode)],
            check=True
        )
    def back(self, times=1):
        """连续返回操作"""
        for _ in range(times):
            self.send_key(4)
            time.sleep(2)  # 模拟人类操作间隔
    
def oneEp(num):
    adb=ADBController()
    for i in range(num):
        adb.tap(520,2208)       #点击合集
        time.sleep(1)
        adb.tap(150,1689)       #点击下一集，
        time.sleep(1)
        adb.tap(508,2060)       #点击免费解锁，
        time.sleep(20)
        adb.tap(999.7,129)     #exit()
        time.sleep(0.5)
        adb.tap(550,1437)     #不再解锁
        time.sleep(1)

adb=ADBController()
# for i in range(5):      #一次解锁5集
#     adb.tap(999.7,129)     #exit()
#     time.sleep(2)
#     adb.tap(577,1274)       #继续解锁
#     time.sleep(17)

# 依赖滑动方案
num=58-47
# for i in range(int(num/5)):
#     print(i)
    # oneEp(num%5)
# for i in range(int(num/5)):
#     adb.tap(520,2208)       #点击合集
#     time.sleep(1)
#     adb.swipe( 511, 1940, 469, 1940-600, duration=380)  #一次划五集
#     time.sleep(2)
#     adb.tap(145,1803)       #点击新一集，
#     time.sleep(2)
#     adb.tap(508,2060)       #点击免费解锁，
#     time.sleep(3)
#     for j in range(5):      #一次解锁5集
#         time.sleep(20)
#         adb.tap(999.7,129)     #exit()
#         time.sleep(1.5)
#         adb.tap(577,1274)       #继续解锁
#     time.sleep(2)
adb.tap(520,2208)       #点击合集
time.sleep(1)
adb.swipe( 511, 1940, 469, 1940-600, duration=380)  #一次划五集 
time.sleep(2)
adb.tap(127,1485)       #点击已解锁的最后一集          
oneEp(num%5)        

# adb.tap(520,2208)       #点击合集
# time.sleep(1)
# adb.swipe( 511, 1940, 469, 1940-600, duration=380)  #一次划五集
# time.sleep(1)
# adb.tap(147,1106)       #点击新一集，


#无滑动方案
# num=48
# for i in range(num):
#     adb.tap(520,2208)       #点击合集
#     time.sleep(1)
#     adb.tap(150,1689)       #点击下一集，
#     adb.tap(508,2060)       #点击免费检索，
#     time.sleep(20)
#     adb.tap(999.7,129)     #exit()
#     time.sleep(0.5)
#     adb.tap(550,1437)     #不再解锁
#     time.sleep(1)



num=7
# if (num/5)>int(num/5):
#     someOnes=num%5
# for i in range(int(num/5)):
#     print(i)
    # oneEp(num%5)
